package com.elminster.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import org.apache.commons.lang.time.DateFormatUtils;

public class DateUtil {
    /** yyyy */
    public static final String YYYY = "yyyy";
    /** MM */
    public static final String MM = "MM";
    /** DD */
    public static final String DD = "dd";
    /** yyyyMM */
    public static final String YYYYMM = "yyyyMM";
    /** yyyyMMdd */
    public static final String YYYYMMDD = "yyyyMMdd";
    /** yyyy/MM/dd */
    public static final String YYYYMMDD_DISP = "yyyy/MM/dd";
    /** yyyy年MM月dd日 */
    public static final String YYYYMMDD_DISP_JAPANESE = "yyyy年MM月dd日";
    /** yyyy/MM/dd HH:mm:ss */
    public static final String YMDHMS_DISP = "yyyy/MM/dd HH:mm:ss";
    /** yyyyMMddHHmmss */
    public static final String YMDHMS = "yyyyMMddHHmmss";
    /** yyyyMMdd_HHmmss */
    public static final String YMD_HMS = "yyyyMMdd_HHmmss";
    /** HH:mm:ss */
    public static final String HMS_DISP = "HH:mm:ss";
    /** HHmmss */
    public static final String HMS = "HHmmss";
    /** yyyyMMddhhmmssSSS */
    public static final String YMDHMSF3 = "yyyyMMddHHmmssSSS";
    /** yyyy/MM/dd hh:mm:ss.SSS */
    public static final String YMDHMSF3_DISP = "yyyy/MM/dd HH:mm:ss.SSS";
    /** yyMMddhhmmssSSS */
    public static final String YYMMDDHMSF3 = "yyMMddHHmmssSSS";
    /** yyyyMMddHHmm */
    public static final String YYYYMMDDHHMM = "yyyyMMddHHmm";
    /** yyMMdd */
    public static final String YYMMDD = "yyMMdd";
    /** GMT+0 */
    private static final String GMT_0 = "GMT+0";
    
    public static String date2String(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            String sysdate = sdf.format(date);
            return sysdate;
        } catch (Exception e) {
            return null;
        }
    }
    
    public static Date string2Date(String dateStr, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(dateStr);
        } catch (Exception e) {
            return null;
        }
    }
    
    public static String getTimeDif(Date before, Date after) {
        Date timeDif = new Date(after.getTime() - before.getTime());
        TimeZone tz = TimeZone.getTimeZone(GMT_0);
        return DateFormatUtils.format(timeDif, HMS_DISP, tz);
    }
}
